<?php

namespace App\Admin\Controllers;

use App\Models\WechatUser;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class WechatUserController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '用户管理';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new WechatUser());

        $grid->column('id', __('ID'))->sortable();
        $grid->column('name', __('姓名'))->editable();
        $grid->column('openid', __('微信ID'));
        $grid->column('is_subscribe', __('是否关注'))->bool([
            0 => false,
            1 => true,
        ]);
        $grid->column('remark', __('备注'))->editable();
        $states = [
            'on'  => ['value' => 1, 'text' => '正常', 'color' => 'success'],
            'off' => ['value' => 0, 'text' => '禁用', 'color' => 'danger'],
        ];

        $grid->column('state', __('状态'))->switch($states);
        $grid->column('create_time', __('创建时间'));
        $grid->column('update_time', __('更新时间'));
        $grid->model()->orderBy('id', 'desc');
        $grid->filter(function ($filter) {
            $filter->disableIdFilter();
            $filter->like('name', __('姓名'))->placeholder('请输入姓名');
            $filter->like('remark', __('备注'))->placeholder('请输入备注');
        });
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(WechatUser::findOrFail($id));

        $show->field('id', __('ID'));
        $show->field('name', __('姓名'));
        $show->field('openid', __('微信ID'));
        $show->field('remark', __('备注'));
        $show->field('state', __('状态'));
        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new WechatUser());

        $form->display('id', __('ID'));
        $form->text('name', __('姓名'));
        $form->display('openid', __('微信ID'));
        $form->text('remark', __('备注'));
        $form->switch('state', __('状态'));

        return $form;
    }
}
